<?php

namespace App\Listeners;

use App\BO\Order\Trace\OrderTraceRecorder;
use App\BO\OrderStatus\Events\OrderCreateSuccessEvent;
use App\BO\OrderStatus\Events\OrderPayExpiredEvent;
use App\BO\OrderStatus\Events\OrderPaySuccessEvent;
use App\BO\OrderStatus\Events\OrderUserApplyRefundEvent;
use App\BO\OrderStatus\Events\OrderUserCancelEvent;
use App\BO\OrderStatus\Events\OrderUserCancelledRefundEvent;

class OrderTraceSubscriber
{
    public const  SUBSCRIBER = OrderTraceSubscriber::class;

    /**
     * 订单创建成功时调用
     * @param OrderCreateSuccessEvent $event
     * @return void
     */
    public function onOrderCreateSuccess(OrderCreateSuccessEvent $event)
    {
        OrderTraceRecorder::addBeginTrace($event->getOrder());
    }

    /**
     * 用户取消
     * @param OrderUserCancelEvent $event
     */
    public function onOrderUserCancel(OrderUserCancelEvent $event)
    {
        OrderTraceRecorder::addUserCancelTrace($event->getOrder());
    }

    /**
     * @param OrderUserApplyRefundEvent $event
     */
    public function onOrderUserApplyRefund(OrderUserApplyRefundEvent $event)
    {
        OrderTraceRecorder::addUserApplyRefundTrace($event->getOrder());
    }

    /**
     * @param OrderUserCancelledRefundEvent $event
     */
    public function onOrderUserCancelledRefund(OrderUserCancelledRefundEvent $event)
    {
        OrderTraceRecorder::addUserCancelledRefundRefundTrace($event->getOrder());
    }

    /**
     * @param OrderPaySuccessEvent $event
     */
    public function onOrderPaySuccess(OrderPaySuccessEvent $event)
    {
        OrderTraceRecorder::addPaySuccessTrace($event->getOrder());
    }

    /**
     * 订单过期末支付
     * @param OrderPayExpiredEvent $event
     */
    public function onOrderPayExpired(OrderPayExpiredEvent $event)
    {
        OrderTraceRecorder::addPayExpiredTrace($event->getOrder());
    }

    /**
     * 为订阅者注册监听器。
     *
     * @param  \Illuminate\Events\Dispatcher  $events
     */
    public function subscribe($events)
    {
        $events->listen(
            OrderCreateSuccessEvent::class,
            self::SUBSCRIBER . '@onOrderCreateSuccess'
        );

        $events->listen(
            OrderUserCancelEvent::class,
            self::SUBSCRIBER . '@onOrderUserCancel'
        );

        $events->listen(
            OrderUserApplyRefundEvent::class,
            self::SUBSCRIBER . '@onOrderUserApplyRefund'
        );

        $events->listen(
            OrderUserCancelledRefundEvent::class,
            self::SUBSCRIBER . '@onOrderUserCancelledRefund'
        );

        $events->listen(
            OrderPaySuccessEvent::class,
            self::SUBSCRIBER . '@onOrderPaySuccess'
        );

        $events->listen(
            OrderPayExpiredEvent::class,
            self::SUBSCRIBER . '@onOrderPayExpired'
        );
    }

}
